package com.example.screenshotlistener;

import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.FileObserver;

import org.jetbrains.annotations.NotNull;

import java.io.File;

public class FileObserverUtils {
    private static ISnapShotCallBack snapShotCallBack;
    private static FileObserver fileObserver;
    private static String SNAP_SHOT_FOLDER_PATH;
    private static String lastShownSnapshot;
    private static int MAX_TRYS = 3;
    public static void setSnapShotCallBack(@NotNull ISnapShotCallBack callBack) {
        snapShotCallBack = callBack;
        initFileObserver();
    }


    public static void startSnapshotWatching() {
        if (fileObserver != null) {
            fileObserver.startWatching();
        }
    }

    public static void stopSnapshotWatching() {
        if (fileObserver != null) {
            fileObserver.stopWatching();
        }
    }

    private static void initFileObserver() {
        SNAP_SHOT_FOLDER_PATH = Environment.getExternalStorageDirectory()
                + File.separator + Environment.DIRECTORY_PICTURES
                + File.separator + "Screenshots" + File.separator;

        fileObserver = new FileObserver(SNAP_SHOT_FOLDER_PATH, FileObserver.CREATE) {
            @Override
            public void onEvent(int event, String path) {
                if (null != path && event == FileObserver.CREATE && (!path.equals(lastShownSnapshot))){
                    lastShownSnapshot = path; // 有些手机同一张截图会触发多个CREATE事件，避免重复展示

                    String snapShotFilePath = SNAP_SHOT_FOLDER_PATH + path;

                    int tryTimes = 0;
                    while (true) {
                        try { // 收到CREATE事件后马上获取并不能获取到，需要延迟一段时间
                            Thread.sleep(600);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            BitmapFactory.decodeFile(snapShotFilePath);
                            break;
                        } catch (Exception e) {
                            e.printStackTrace();
                            tryTimes++;
                            if (tryTimes >= MAX_TRYS) { // 尝试MAX_TRYS次失败后，放弃
                                return;
                            }
                        }
                    }

                    snapShotCallBack.snapShotTaken(path);
                }
            }
        };
    }
}
